package app

import (
	"fmt"

	"github.com/gin-gonic/gin"

	"github.com/EDDYCJY/go-gin-example/pkg/e"
)

type Gin struct {
	C *gin.Context
}

type Response struct {
	Code int         `json:"ResponseStatus"`
	Msg  string      `json:"ResponseMsg"`
	Data interface{} `json:"datas"`
}

type QmResponse struct {
	Code      string `json:"code"`
	Msg       string `json:"error_info"`
	ErrorCode string `json:"error_code"`
}

// Response setting gin.JSON
func (g *Gin) Response(httpCode, errCode int, data interface{}) {
	if errCode == 200 {
		errCode = 0
	}
	g.C.JSON(200, Response{
		Code: errCode,
		Msg:  e.GetMsg(errCode),
		Data: data,
	})
}

// Response setting gin.JSON
func (g *Gin) ResponseMsg(errMsg error, data interface{}) {
	x := fmt.Sprintf("%s", errMsg)
	g.C.JSON(200, Response{
		Code: 100,
		Msg:  x,
		Data: data,
	})
}

// Response setting gin.JSON
func (g *Gin) QmResponseMsg(errMsg error, errorCode string, data interface{}) {
	x := fmt.Sprintf("%s", errMsg)
	if errorCode != "" {
		errorCode = "sign-check-failure"
	}
	g.C.JSON(200, QmResponse{
		Code:      "0",
		Msg:       x,
		ErrorCode: errorCode,
	})
}

func (g *Gin) ResponseMessage(errMsg string, data interface{}) {
	g.C.JSON(200, Response{
		Code: 103,
		Msg:  errMsg,
		Data: data,
	})
}
